package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
 @Reference
 private RoleService roleService;

 //用户新增时候 查询所有角色信息
 @RequestMapping("/findAll")
 @PreAuthorize("hasAuthority('ROLE_QUERY')")
 public Result findAll() {
  log.debug("RoleController-findAll");
  List<Role> list = roleService.findAll();//发送请求
  log.debug("roleList: { " + list + " }");
  return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, list);
 }

 //分页查询角色信息
 @RequestMapping("/findPage")
 @PreAuthorize("hasAuthority('ROLE_QUERY')")
 public PageResult findPage(@RequestBody QueryPageBean pageBean) {
  log.debug("RoleController-findPage: QueryPageBean{ " + pageBean + " }");
  PageResult pageResult = roleService.findPage(pageBean);
  log.debug("RoleController-findPage: PageResult {" + pageResult + " }");
  return pageResult;
 }

 //新增角色
 @RequestMapping("/add")
 @PreAuthorize("hasAuthority('ROLE_ADD')")
 public Result add(@RequestBody Map<String, Object> map) {
  log.debug("map: {" + map.toString() + "}");
  //把map集合反序列化为 对象
  //权限ids
  JSONArray jsonArray1 = (JSONArray) map.get("permissionIds");
  //把jsonArray转换为数组 或者是集合
  Integer[] permissionIds = jsonArray1.toArray(new Integer[]{});
  log.debug("permissionIds:" + Arrays.toString(permissionIds));
  //菜单ids
  JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
  //把jsonArray转换为数组 或者是集合
  Integer[] menuIds = jsonArray2.toArray(new Integer[]{});
  //角色
  JSONObject jsonObject = (JSONObject) map.get("role");
  Role role = jsonObject.toJavaObject(Role.class);
  log.debug("menuIds:" + Arrays.toString(menuIds));
  log.debug("role:" + role);
  roleService.add(role, permissionIds, menuIds);
  log.debug(MessageConst.ADD_ROLE_SUCCESS);
  return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
 }

 //根据id查询角色信息
 @RequestMapping("/findById")
 @PreAuthorize("hasAuthority('ROLE_QUERY')")
 public Result findById(Integer id) {
  log.debug("RoleController-findById: id{ " + id + " }");
  Role role = roleService.findById(id);
  log.debug("RoleController-findById: role{ " + role + " }");
  return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, role);
 }

 //根据检roleid查询关联的permissionids
 @RequestMapping("/findPermissionIdsByRoleId")
 @PreAuthorize("hasAuthority('ROLE_QUERY')")
 public Result findCheckItemIdsByCheckGroupId(Integer roleId) {
  log.debug("roleController-findPermissionIdsByRoleId: PermissionId: { " + roleId + " }");
  List<Integer> list = roleService.findPermissionIdsByRoleId(roleId);
  log.debug("roleList: { " + list + " }");
  return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, list);
 }

 //根据检roleid查询关联的menuids  //findMenuIdsByRoleId
 @RequestMapping("/findMenuIdsByRoleId")
 @PreAuthorize("hasAuthority('ROLE_QUERY')")
 public Result findMenuIdsByRoleId(Integer roleId) {
  log.debug("roleController-findMenuIdsByRoleId: MenuId: { " + roleId + " }");
  List<Integer> list = roleService.findMenuIdsByRoleId(roleId);
  log.debug("roleList: { " + list + " }");
  return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, list);
 }

 //编辑角色信息
 @RequestMapping("/edit")
 @PreAuthorize("hasAuthority('ROLE_EDIT')")
 public Result edit(@RequestBody Map<String, Object> map) {
  log.debug("map: {" + map.toString() + "}");
  //把map集合反序列化为 对象
  //权限ids
  JSONArray jsonArray1 = (JSONArray) map.get("permissionIds");
  //把jsonArray转换为数组 或者是集合
  Integer[] permissionIds = jsonArray1.toArray(new Integer[]{});
  log.debug("permissionIds:" + Arrays.toString(permissionIds));
  //菜单ids
  JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
  //把jsonArray转换为数组 或者是集合
  Integer[] menuIds = jsonArray2.toArray(new Integer[]{});
  log.debug("menuIds:" + Arrays.toString(menuIds));
  //角色
  JSONObject jsonObject = (JSONObject) map.get("role");
  Role role = jsonObject.toJavaObject(Role.class);
  log.debug("role:" + role);
  roleService.edit(role, permissionIds, menuIds);
  log.debug(MessageConst.EDIT_ROLE_SUCCESS);
  return new Result(true, MessageConst.EDIT_ROLE_SUCCESS);
 }

 //根据id删除角色
 @RequestMapping("/delById")
 @PreAuthorize("hasAuthority('ROLE_DELETE')")
 public Result delById(Integer id) {
  log.debug("delById : { " + id + "}");
  roleService.delById(id);
  log.debug(MessageConst.DELETE_ROLE_SUCCESS);
  return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);
 }
}
